<template>
  <div class="app-container">
    <div class="mb10">
      <el-button type="success" icon="el-icon-plus" @click="handleAdd"> 添加 </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      size="mini"
    >
      <el-table-column label="#" type="index" width="50" />
      <el-table-column label="账户" prop="account" />
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="手机" prop="mobile" />
      <el-table-column label="电邮" prop="email" />
      <el-table-column label="状态" prop="status">
        <template slot-scope="{ row }"
          >{{ statusOptionsKV[row.status] }}
        </template></el-table-column
      >
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
          />
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-lock"
            @click="handleReset(row)"
            >重置密码</el-button
          >
          <!-- <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          /> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="m-center">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchList"
      />
    </div>

    <!-- 编辑 -->
    <el-dialog
      :title="isEdit ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="500px"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="账户" prop="account" required>
          <el-input v-model="temp.account"></el-input>
        </el-form-item>
        <el-form-item v-if="!isEdit" label="初始密码" prop="password" required>
          <el-input v-model="temp.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name" required>
          <el-input v-model="temp.name"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile" required>
          <el-input v-model="temp.mobile"></el-input>
        </el-form-item>
        <el-form-item label="电邮" prop="email" required>
          <el-input v-model="temp.email"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" required>
          <el-select v-model="temp.status" placeholder="请选择状态">
            <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="isEdit ? update() : add()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createCustomerAccount,
  queryCustomerAccount,
  modifyCustomerAccount,
  modifyCustomerAccountPassword,
  modifyCustomerAccountStatus,
} from "@/api/client";
import { fetchList as getUsers } from "@/api/user";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaultForm = {
  mobile: "",
  name: "",
  email: "",
  password: "",
  account: "",
  customerId: "",
  status: "",
};

export default {
  name: "CustomerAccount",
  components: { Pagination },
  filters: {},
  computed: {
    statusOptionsKV: function () {
      return this.statusOptions.reduce((acc, cur) => {
        acc[cur.key] = cur.value;
        return acc;
      }, {});
    },
  },
  data() {
    return {
      tableData: [],
      total: 0,
      listQuery: {
        customerId: "",
        limit: 10,
        page: 1,
      },
      statusOptions: [
        { key: "0", value: "In Service" },
        { key: "1", value: "Out Service" },
      ],
      temp: Object.assign({}, defaultForm),
      dialogVisible: false,
      rules: {},
      listLoading: false,
      isEdit: false,
    };
  },

  created() {
    const id = this.$route.params && this.$route.params.id;
    if (id) {
      this.listQuery.customerId = parseInt(id);
      this.temp.customerId = parseInt(id);
      // this.getUsers();
      this.fetchList();
    }
  },
  methods: {
    // 得到列表
    async fetchList() {
      this.listLoading = true;
      queryCustomerAccount(this.listQuery).then((response) => {
        this.tableData = response.data.items;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    // 搜索
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchList();
    },

    resetTemp() {
      this.temp = Object.assign({}, defaultForm);
      this.temp.customerId = this.listQuery.customerId;
    },
    handleAdd() {
      this.resetTemp();
      this.dialogVisible = true;
      this.isEdit = false;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    add() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createCustomerAccount(this.temp).then((response) => {
            this.$message.success("添加成功");
            this.dialogVisible = false;
            this.fetchList();
          });
        }
      });
    },
    handleUpdate(row) {
      this.temp = Object.assign(this.temp, row);
      this.isEdit = true;
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    // viewServiceOfficer(id) {
    //   viewServiceOfficer({ id }).then((response) => {
    //     this.temp = Object.assign(this.temp, response.data);

    //     this.isEdit = true;
    //     this.dialogVisible = true;
    //     this.$nextTick(() => {
    //       this.$refs["dataForm"].clearValidate();
    //     });
    //   });
    // },
    // 修改
    update() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          modifyCustomerAccount(this.temp).then((response) => {
            this.$message.success("修改成功");
            this.dialogVisible = false;
            this.fetchList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm(`你确定要删除【${row.name}】吗?`, "提示", {
        type: "warning",
      })
        .then(() => {
          // deleteContact { id: row.id }.then(() => {
          //   this.$message.success("删除成功");
          //   this.fetchList();
          // });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    handleReset(row) {
      this.$confirm(`你确定要重置【${row.name}】的密码吗?`, "提示", {
        type: "warning",
      })
        .then(() => {
          modifyCustomerAccountPassword({ id: row.id, password: "666666" }).then(() => {
            this.$message.success("密码已重置为666666");
            this.fetchList();
          });
        })
        .catch(() => {
          this.$message.info("已取消操作");
        });
    },
  },
};
</script>
